const foodFacade = require('../facade/food.server.facade.js');
const foodTypeFacade = require('../facade/foodType.server.facade.js');
const _ = require('lodash');
var formidable = require('formidable');
var config = require('../../config/env/config_development.js');
var _f=require('../util/file');
var ep = require('../util/event_proxy');


module.exports = {
    addFoodItem: addFoodItem,
    delFoodItemByFoodId: delFoodItemByFoodId,
    modifyFoodItemByFoodItemId: modifyFoodItemByFoodItemId,
    getAllFoodItems:getAllFoodItems,
    getAllFoodItemsByMerchantId: getAllFoodItemsByMerchantId,
    getFoodItemByFoodItemId: getFoodItemByFoodItemId,
    getAllFoodItemsByFoodTypeWithMerchantId: getAllFoodItemsByFoodTypeWithMerchantId,
}



function addFoodItem(req, res, next) {


    var form = new formidable.IncomingForm();
    _f.uploadSetting(form);

    form.parse(req, function (err, fields, files) {

        var food = fields;

        foodFacade.addFoodItem(req.session.merchant._id, food, function (err, result) {
            if (err) {
                return next(error);
            }
            console.log(files)
            var fileName=result.food_id+'.jpg';

            _f.rename(files.img.path,config.upload.localFood+fileName);

            return ep.emit('success', res, result, "Add food Success!");

        })
    });

}

function delFoodItemByFoodId(req, res, next) {
    let params = req.params;
    let foodId = params.foodId;

    if (_.isEmpty(foodId)) {
        return ep.emit('err', res, 422, "Food id is null");
    }

    foodFacade.delFoodItemByFoodId(foodId, function getFoodTypeCB(error, result) {
        if (error) {
            return next(error);
        }
        return ep.emit("success",res,result,"Delete Success!");
    });

}

function getAllFoodItemsByFoodTypeWithMerchantId(req, res, next) {

    let params = req.params;
    let foodType = params.foodType;
    let merchant_id = params.merchantId;

    if (_.isEmpty(foodType)) {
        return ep.emit('err', res, 422, "foodType id is null");
    }

    if (_.isEmpty(merchant_id)) {
        return ep.emit('err', res, 422, "merchant id is null");
    }
    foodFacade.getAllFoodItemsByFoodType(foodType,merchant_id, function getFoodTypeCB(error, result) {
        if (error) {
            return next(error);
        }

        return ep.emit("success",res,result,"Get Success!");

    });

}


function getFoodItemByFoodItemId(req, res, next) {
    let params = req.params;
    let foodId = params.foodId;

    if (_.isEmpty(foodId)) {
        return ep.emit('err', res, 422, "Food id is null");
    }

    foodFacade.getFoodItemByFoodItemId(foodId, function getFoodTypeCB(error, result) {
        if (error) {
            return next(error);
        }

        return ep.emit("success",res,result,"Get Success!");

    });

}

function modifyFoodItemByFoodItemId(req, res, next) {

    var form = new formidable.IncomingForm();
    _f.uploadSetting(form);

    form.parse(req, function (err, fields, files) {
        var food = fields;
        console.log(food);
        foodFacade.modifyFoodItemByFoodItemId(food, function (err, result) {
            if (err) {
                return next(error);
            }
            var fileName=food.food_id+'.jpg';
            _f.rename(files.img.path,config.upload.localFood+fileName);

            foodFacade.getFoodItemByFoodItemId(food.food_id,function (err,result) {

                if (err) {
                    return next(error);
                }

                result.img = result.img + "?"+Date.parse(new Date());

                console.log("modify"+result);

                return ep.emit("success",res,result,"Modify Success!");

            });

        })
    });
}

function delFoodItemByFoodId(req, res, next) {
    let params = req.params;
    let foodId = params.foodId;

    if (_.isEmpty(foodId)) {
        return ep.emit('err', res, 422, "Food id is null");
    }

    foodFacade.delFoodItemByFoodId(foodId, function getFoodCB(error, result) {
        if (error) {
            return next(error);
        }

        return ep.emit("success",res,result,"Delete Success!");
    });
}

function getAllFoodItems(req, res, next) {

    var merchant_id = req.session.merchant._id;

    foodFacade.getAllFoodItemsByMerchantId(merchant_id,function getAllFoodItemsByMerchantIdCB(error, result) {
        if (error) {
            return next(error);
        }

        return ep.emit("success",res,result,"Get Success!");

    });

}


function getAllFoodItemsByMerchantId(req, res, next) {

    var merchant_id = req.params.merchantId;

    if (_.isEmpty(merchant_id)) {
        return ep.emit('err', res, 422, "Merchant id is null");
    }

    foodFacade.getAllFoodItemsByMerchantId(merchant_id,function getAllFoodItemsByMerchantIdCB(error, result) {
        if (error) {
            return next(error);
        }

        return ep.emit("success",res,result,"Get Success!");
    });

}

